#ifndef DISPLAY_TM1637_H
#define DISPLAY_TM1637_H

#include <Arduino.h>
#include <TM1637Display.h>
#include "component_dht11.h"
#include "component_ultrasonic.h"

float PreviousValue = 0;

// Empty. Things are done in Tasks.
int CLK = 7;
int DIO = 8;
TM1637Display display(CLK, DIO);

// degree celsius symbol
const uint8_t celsius[] = {
    SEG_A | SEG_B | SEG_F | SEG_G, // Degree symbol
    SEG_A | SEG_D | SEG_E | SEG_F  // C
};

void display_info()
{
  if (dht11_state == DHT11_ON)
  {
    if (temperature != PreviousValue)
    {
      PreviousValue = temperature;
      display.showNumberDec(temperature, false, 2, 0);
      display.setSegments(celsius, 2, 2);
    }
  };

  if (dht11_state == DHT11_OFF)
  {
    display.showNumberDec(distance, false, 3, 0);
  }
};

void setup_display_tm1637()
{
  display.clear();
  display.setBrightness(2); // set the brightness to 7 (0:dimmest, 7:brightest)
  // dht11
  dht11_on();
  if (dht11_state == DHT11_ON)
  {
    display.showNumberDec(temperature, false, 2, 0);
    display.setSegments(celsius, 2, 2);
    PreviousValue = temperature;
  };
  if (dht11_state == DHT11_OFF)
  {
    display.showNumberDec(distance, false, 3, 0);
  }
};

void loop_display_tm1637()
{
  display_info();
}

#endif /* DISPLAY_TM1637 */